package com.jinbo.footmessageworker.order;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.jinbo.footmessageworker.BaseActivity;
import com.jinbo.footmessageworker.MyApplication;
import com.jinbo.footmessageworker.R;
import com.jinbo.footmessageworker.httpbean.OrderBean;
import com.jinbo.footmessageworker.httpbean.OrdersBean;
import com.jinbo.footmessageworker.httpretrofit.BusinessController;
import com.jinbo.footmessageworker.httpretrofit.Listener;
import com.jinbo.footmessageworker.utils.LocalPreferencesHelper;
import com.jinbo.footmessageworker.view.TitleBarView;

import java.util.List;

public class HistoryOrderActivity extends BaseActivity implements View.OnClickListener,AbsListView.OnScrollListener{
    private ListView listView;
    List<OrderBean> orders;
    HistoryOrderListViewAdapter adapter;
    private View footerView;
    private View loading;//尾部正在在载的布局
    private View finishLoad;
    private TextView finishText;

    String user_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_order);
        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("历史订单");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        user_id= MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        footerView = LayoutInflater.from(this).inflate(R.layout.listview_footer, null);
        loading = footerView.findViewById(R.id.loading);//加载中，表尾巴
        finishLoad=footerView.findViewById(R.id.load_finish);
        finishText=(TextView)footerView.findViewById(R.id.load_all_text);

        listView = (ListView)findViewById(R.id.mainListView);//列表
        adapter=new HistoryOrderListViewAdapter(this,orders);
        listView.addFooterView(footerView);
        listView.setAdapter(adapter);

        
        BusinessController.getInstance().historyOrder(user_id, 0, 10, new Listener<OrdersBean>() {
            @Override
            public void onStart(Object... params) {
                showProgressDialog("请稍后...");
                //mLoadView.setVisibility(View.VISIBLE);
                //mPullRefreshLayout.setRefreshing(true);
                //mLoadView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onComplete(OrdersBean result, Object... params) {
                dismissProgressDialog();
                finishLoad.setVisibility(View.GONE);
                listView.setOnScrollListener(HistoryOrderActivity.this);
                orders=result.orders;
                adapter.setorders(orders);
                if (result.orders.size() < 10) {
                    //   showToast("数据已加载完");
                    //   listView.removeFooterView(footerView);
                    loading.setVisibility(View.GONE);
                    finishLoad.setVisibility(View.VISIBLE);
                    listView.setOnScrollListener(null);
                    if(result.orders.size()==0)
                        finishText.setText("暂无订单");
                }
                // mPullRefreshLayout.setRefreshing(false);
                //mLoadView.setVisibility(View.GONE);
               // mCallBacks2.change(result.orders);
                // OrderActivity.this.
               // orders = result.orders;
                // mCallBacks2.change(result.orders);


            }

            @Override
            public void onFail(final String msg, Object... params) {
               // mLoadView.setVisibility(View.VISIBLE);
                dismissProgressDialog();
                //mLoadView.setVisibility(View.GONE);
                // mPullRefreshLayout.setRefreshing(false);
                if (TextUtils.isEmpty(msg)) {
                } else {
                    show(msg);
                }
            }
        });
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // 当不滚动时
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            // 判断是否滚动到底部
            if (view.getLastVisiblePosition() == view.getCount() - 1) {
                // 加载更多
                // loadMoreData();
                //loading.setVisibility(View.VISIBLE);
                BusinessController.getInstance().historyOrder(user_id, orders.size(), 10, new Listener<OrdersBean>() {
                    @Override
                    public void onStart(Object... params) {
                        loading.setVisibility(View.VISIBLE);
                       // showProgressDialog("请稍后...");
                        //mLoadView.setVisibility(View.VISIBLE);
                        //mPullRefreshLayout.setRefreshing(true);
                        //mLoadView.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onComplete(OrdersBean result, Object... params) {
                        //dismissProgressDialog();
                        loading.setVisibility(View.GONE);

                        orders.addAll(result.orders);
                        adapter.setorders(orders);
                        if (result.orders.size() < 10) {
                            //   showToast("数据已加载完");
                            //   listView.removeFooterView(footerView);
                            loading.setVisibility(View.GONE);
                            finishLoad.setVisibility(View.VISIBLE);
                            listView.setOnScrollListener(null);
                        }
                        // mPullRefreshLayout.setRefreshing(false);
                        //mLoadView.setVisibility(View.GONE);
                        // mCallBacks2.change(result.orders);
                        // OrderActivity.this.
                        // orders = result.orders;
                        // mCallBacks2.change(result.orders);


                    }

                    @Override
                    public void onFail(final String msg, Object... params) {
                        // mLoadView.setVisibility(View.VISIBLE);
                        //dismissProgressDialog();
                        //mLoadView.setVisibility(View.GONE);
                        // mPullRefreshLayout.setRefreshing(false);
                        loading.setVisibility(View.GONE);
                        if (TextUtils.isEmpty(msg)) {
                        } else {
                            show(msg);
                        }
                    }
                });
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
        }
    }
}
